var form, $,areaData;
layui.config({
    base : "../../js/base/"
}).extend({
    "address" : "address"
})
layui.use(['form','layer','upload','laydate',"address"],function(){
    form = layui.form;
    $ = layui.jquery;
    var layer = parent.layer === undefined ? layui.layer : top.layer,
        upload = layui.upload,
        laydate = layui.laydate,
        address = layui.address;

    //填充省份信息，同时调取市级信息列表
    var province=$(".province").attr("data-value");
    var city=$(".city").attr("data-value");
    var area=$(".area").attr("data-value");
    address.provinces();
    var citys;
    $.get("../js/json/address.json", function (addressData) {
        $(".userAddress select[name='province']").val(province); //省
        var value = province;
        if (value > 0) {
            address.citys(addressData[$(".userAddress select[name='province'] option[value='"+province+"']").index()-1].childs);
            citys = addressData[$(".userAddress select[name='province'] option[value='"+province+"']").index()-1].childs;
        } else {
            $('.userAddress select[name=city]').attr("disabled","disabled");
        }
        $(".userAddress select[name='city']").val(city); //市
        //填充市级信息，同时调取区县信息列表
        var value = city;
        if (value > 0) {
            address.areas(citys[$(".userAddress select[name=city] option[value='"+city+"']").index()-1].childs);
        } else {
            $('.userAddress select[name=area]').attr("disabled","disabled");
        }
        $(".userAddress select[name='area']").val(area); //区
        form.render();
    })

    //上传头像
    upload.render({
        elem: '.userFaceBtn',
        url: '/file/picture',
        method : "post",  //此处是为了演示之用，实际使用中请将此删除，默认用post方式提交
        done: function(res, index, upload){
            debugger;
            if(res.code=="success"){
                $('.userAvatar').attr('src',res.result);
            }else{
                offset("auto",res.errorMsg,"温馨提示","确定");
            }
        }
    });

    //添加验证规则
    form.verify({
        userBirthday : function(value){
            if(!/^(\d{4})[\u4e00-\u9fa5]|[-\/](\d{1}|0\d{1}|1[0-2])([\u4e00-\u9fa5]|[-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/.test(value)){
                return "出生日期格式不正确！";
            }
        }
    })
    //选择出生日期
    laydate.render({
        elem: '.brithday',
        format: 'yyyy年MM月dd日',
        trigger: 'click',
        max : 0,
        mark : {"0-12-15":"生日"},
        done: function(value, date){
            if(date.month === 12 && date.date === 15){ //点击每年12月15日，弹出提示语
                layer.msg('今天是马哥的生日，也是layuicms2.0的发布日，快来送上祝福吧！');
            }
        }
    });

    //获取省信息
    address.provinces();

    //提交个人资料
    form.on("submit(changeUser)",function(data){
        var index = layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            type: "POST",
            url: "/baseUser/Update",
            data: {"userid": $('#userid').val(),
                 "username":$('#username').val(),
                "sex":data.field.sex,
                "phone":$('.phone').val(),
                "brithday":$('.brithday').val(),
                'province' : data.field.province,
                'city' : data.field.city,
                'area' : data.field.area,
                'email':$('.email').val(),
                "headLogo":$(".userAvatar").attr("src")},
            success: function(result){
                layer.close(index);
                if(1==result.returnInfo.state){
                    offset("auto","信息修改成功","温馨提示","确定");
                }else{
                    offset("auto",result.returnInfo.errorMsg,"温馨提示","确定");
                }
            },
            error: function(result){
                layer.close(index);
                offset("auto",result.returnInfo.errorMsg,"温馨提示","确定");
            }
        });
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    })

    //修改密码
    form.on("submit(changePwd)",function(data){
        var index = layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
        setTimeout(function(){
            layer.close(index);
            layer.msg("密码修改成功！");
            $(".pwd").val('');
        },2000);
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    })
})